//
//  PhoneLoginController.m
//  EncryPhone
//
//  Created by zhibao on 2022/9/26.
//

#import "LoginController.h"
#import "LoginCodeInputController.h"
@interface LoginController (){
    QMUITextField *phoneField;
    UIButton *nextBtn;
}
@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

-(void)initView{
    UILabel *clabel = [UILabel new];
    clabel.text = LocalizedString(@"欢迎登录");
    clabel.font = MY_SCALE_RegularFONT(25);
    clabel.textColor = UIColor.whiteColor;
    [self.view addSubview:clabel];
    [clabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(pixw(30));
        make.right.mas_equalTo(pixw(-30));
        make.top.mas_equalTo(pixw(103 - 64) + NAVIH);
    }];
    
    UILabel *cplabel = [UILabel new];
    cplabel.text = LocalizedString(@"未注册的手机号验证后将自动创建账户");
    cplabel.font = MY_SCALE_RegularFONT(13);
    cplabel.textColor = MyColor(@"999999");
    cplabel.numberOfLines = 0;
    cplabel.lineBreakMode = NSLineBreakByWordWrapping;
    [self.view addSubview:cplabel];
    [cplabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(clabel);
        make.top.equalTo(clabel.mas_bottom).offset(pixw(8));
    }];
    
    phoneField = [[QMUITextField alloc]init];
    phoneField.placeholder = LocalizedString(@"请输入手机号");
    phoneField.placeholderColor = MyColor(@"ffffff");
    phoneField.textColor = UIColor.whiteColor;
    phoneField.backgroundColor = MyColor(@"bababa");
    phoneField.layer.cornerRadius = 5;
    phoneField.textInsets = UIEdgeInsetsMake(pixw(4), pixw(10), pixw(4), pixw(10));
    phoneField.font = MY_SCALE_BoldFont(14);
    [phoneField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:phoneField];
    [phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cplabel.mas_bottom).offset(pixw(61));
        make.height.mas_equalTo(pixw(40));
        make.left.right.equalTo(clabel);
    }];
    [phoneField becomeFirstResponder];
    
    UIView *line = [UIView new];
    line.backgroundColor = MyColor(@"#303339");
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(cplabel);
        make.top.equalTo(phoneField.mas_bottom).offset(pixw(3));
        make.height.mas_equalTo(pixw(0.5));
    }];
    
    nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.layer.cornerRadius = pixw(20);
    nextBtn.backgroundColor = ciThemeColor;
    [self.view addSubview:nextBtn];
    [nextBtn setTitle:LocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.titleLabel.font = MY_SCALE_SemiboldFONT(15);
    [nextBtn setTitleColor:MyColor(@"ffffff") forState:UIControlStateNormal];
    nextBtn.userInteractionEnabled = NO;
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(cplabel);
        make.top.equalTo(line.mas_bottom).offset(pixw(30));
        make.height.mas_equalTo(pixw(60));
    }];
    
    YYLabel *bottomLabel = [YYLabel new];
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    bottomLabel.numberOfLines = 0;
    bottomLabel.lineBreakMode = NSLineBreakByWordWrapping;
    bottomLabel.preferredMaxLayoutWidth = MY_Screen_W - pixw(60);
    [self.view addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(line);
        make.bottom.equalTo(self.view).offset(pixw(-30));
    }];
    
    NSMutableString *string = [[NSMutableString alloc]initWithString:LocalizedString(@"登录即代表同意")];
    [string appendString:@"《"];
    [string appendString:LocalizedString(@"用户协议")];
    [string appendString:@"》"];
    [string appendString:LocalizedString(@"和")];
    [string appendString:@"《"];
    [string appendString:LocalizedString(@"隐私政策")];
    [string appendString:@"》"];
    NSRange yhrange = [string rangeOfString:LocalizedString(@"用户协议")];
    NSRange xyrange = [string rangeOfString:LocalizedString(@"隐私政策")];
    
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
    attri.yy_font = MY_SCALE_RegularFONT(11);
    attri.yy_color = UIColor.whiteColor;
    __weak typeof(self) weakSelf = self;
    [attri yy_setTextHighlightRange:NSMakeRange(yhrange.location - 1, yhrange.length + 2) color:MYBlue backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"用户协议") WithUrl:@"https://docs.qq.com/doc/DRmNvbVFaZm9LVG9t"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    [attri yy_setTextHighlightRange:NSMakeRange(xyrange.location - 1, xyrange.length + 2) color:MYBlue backgroundColor:UIColor.clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        WebViewController *controller = [[WebViewController alloc] initWithtitle:LocalizedString(@"隐私协议") WithUrl:@"https://docs.qq.com/doc/DRmNvbVFaZm9LVG9t"];
        [strongSelf.navigationController pushViewController:controller animated:YES];
    }];
    bottomLabel.attributedText = attri;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
}
-(void)nextClick{
    if(phoneField.text.qmui_trim.length < 6){
        [QMUITips showError:LocalizedString(@"请输入正确手机号！")];
        return;
    }
        
    NSDictionary *dic =@{
        @"phone":phoneField.text,
        @"type":@"0"
    };
    [QMUITips showLoadingInView:self.view];
    [[HttpHelper sharedInstance] sendPost:@"api/short/message" parameters:dic success:^(NSURLSessionTask * _Nonnull operation, ResponseModel * _Nonnull responseObject) {
        DLog(@"code===%@",responseObject.data);
        [QMUITips hideAllTipsInView:self.view];
        if (responseObject.code == 1) {
            LoginCodeInputController *controller = [LoginCodeInputController new];
            controller.type = 0;
            controller.phone = self->phoneField.text.qmui_trimAllWhiteSpace;
            [self.navigationController pushViewController:controller animated:YES];
        }else{
            [QMUITips showError:responseObject.message];
        }
        
    } failure:^(NSURLSessionTask * _Nonnull operation, NSError * _Nonnull error) {
        [QMUITips hideAllTipsInView:self.view];
    }];
}

-(void)textChange:(QMUITextField *)field{
    DLog(@"wwww===%@",field.text);
    if (field.text.qmui_trimAllWhiteSpace.length < 2) {
        nextBtn.backgroundColor = ciThemeColor;
        nextBtn.userInteractionEnabled = NO;
    }else{
        nextBtn.backgroundColor = MYRed;
        nextBtn.userInteractionEnabled = YES;
    }
}

@end
